#{extends 'main.html' /} #{set title:'Browse' /}
<head>
<script type="text/javascript">
	$(function() {
		$('#entOptions').hide();
		$('#entOptions').slideDown(1000);
		javascript: showEntitsJoined();
		$('#joinedEntits').hide();
		javascript: showEntitsManaged();
		$('#managedEntits').hide();
		javascript: showEntitsFollowed();
		$('#followedEntits').hide();
	});
</script>

<!-- ----------------------------------------javascript for browsing Entities-------------------------------------------------- -->
<script type="text/javascript">
	function showEntitsJoined() {
		var user = '${user}';
		$
				.getJSON(
						'@{BrowseController.browseJoinedEntities()}',
						{
							'userName' : user
						},
						function(data) {
							var entits = data.joinedEntits;
							var entitsPart = "";
							if (entits == null || entits == "") {
								document.getElementById("joinedEntits").innerHTML = "No entities to show";
							} else {
								entitsPart = "";
								var x = entits.split("&&");
								for ( var i = 0; i < x.length - 1; i = i + 1) {
									entitsPart += "<a href=\"@{Application.entityViewByName}?entityName=" + x[i] +"\">" + x[i] +  "</a> ";
									entitsPart += "<a href = '#' onclick = \"getInsideEntit('"
											+ ""
											+ user
											+ "','"
											+ x[i]
											+ "')\">" + "expand" + "</a>";
									entitsPart += "<div id = \"entit" + x[i] + "\"></div>";
								}
								document.getElementById("joinedEntits").innerHTML = entitsPart;
							}
						});
	}
</script>

<script type="text/javascript">
	function showEntitsManaged() {
		var user = '${user}';
		$
				.getJSON(
						'@{BrowseController.browseManagedEntities()}',
						{
							'userName' : user
						},
						function(data) {
							var entits = data.managedEntits;
							var entitsPart = "";
							if (entits == null || entits == "") {
								document.getElementById("managedEntits").innerHTML = "No entities to show";
							} else {
								entitsPart = "";
								var x = entits.split("&&");
								for ( var i = 0; i < x.length - 1; i = i + 1) {
									entitsPart += "<a href=\"@{Application.entityViewByName}?entityName=" + x[i] +"\">" + x[i] +  "</a> ";
									entitsPart += "<a href = '#' onclick = \"getInsideEntit('"
											+ ""
											+ user
											+ "','"
											+ x[i]
											+ "')\">" + "expand" + "</a>";
									entitsPart += "<div id = \"entit" + x[i] + "\"></div>";
								}
								document.getElementById("managedEntits").innerHTML = entitsPart;
							}
						});
	}
</script>

<script type="text/javascript">
	function showEntitsFollowed() {
		var user = '${user}';
		$
				.getJSON(
						'@{BrowseController.browseFollowedEntities()}',
						{
							'userName' : user
						},
						function(data) {
							var entits = data.followedEntits;
							var entitsPart = "";
							if (entits == null || entits == "") {
								document.getElementById("followedEntits").innerHTML = "No entities to show";
							} else {
								entitsPart = "";
								var x = entits.split("&&");
								for ( var i = 0; i < x.length - 1; i = i + 1) {
									entitsPart += "<a href=\"@{Application.entityViewByName}?entityName=" + x[i] +"\">" + x[i] +  "</a> ";
									entitsPart += "<a href = '#' onclick = \"getInsideEntit('"
											+ ""
											+ user
											+ "','"
											+ x[i]
											+ "')\">" + "expand" + "</a>";
									entitsPart += "<div id = \"entit" + x[i] + "\"></div>";
								}
								document.getElementById("followedEntits").innerHTML = entitsPart;
							}
						});
	}
</script>
<script type="text/javascript">
	function getInsideEntit(u, entit) {
		var user = String(u);
		var entity = String(entit);
		$
				.getJSON(
						'@{BrowseController.showEntitContents()}',
						{
							'entityName' : entity,
							'userName' : user
						},
						function(data) {
							var entit = "";
							var topics = "";
							var desiredEntity = "entit" + entity;
							if (data != null && data.entities != "") {
								var x = data.entities.split("&&");
								for ( var i = 0; i < x.length - 1; i = i + 1) {
									entit += "<a href=\"@{Application.entityViewByName}?entityName=" + x[i] +"\">" + x[i] +  "</a> ";
									entit += "<a href = '#' onclick = \"getInsideEntit('"
											+ ""
											+ user
											+ "','"
											+ x[i]
											+ "')\">" + "expand" + "</a></p>";
									entit += "<div id = \"entit" + x[i] + "\"></div>";
								}
							} else {
								alert("No entities inside the entity");
							}

							if (data != null && data.topics != "") {
								var y = data.topics.split("&&");
								for ( var j = 0; j < y.length - 1; j = j + 1) {
									var id = y[j].split("-")[0];
									var title = y[j].split("-")[1];
									topics += "<p><a href=\"@{TopicIdeaController.checkAuthorizationToViewTopic()}?topicId="
											+ id + "\">" + title + "</a>";
									topics += "<a href = '#' onclick = \"getInsideTopic('"
											+ ""
											+ user
											+ "','"
											+ y[j]
											+ "')\">" + "expand" + "</a></p>";
									topics += "<div id = \"topic" + y[j] + "\"></div>";
								}

							} else {
								alert("No topics inside the entity");
							}

							document.getElementById(desiredEntity).innerHTML = topics
									+ entit;
							$('#' + desiredEntity + '').hide();
							$('#' + desiredEntity + '').slideDown(1000);
						});
	}
</script>
<script type="text/javascript">
	function getInsideTopic(userName, topic) {
		var user = userName;
		var topicId = (topic.split("-"))[0];
		var desiredTopic = "topic" + topic;
		$
				.getJSON(
						'@{BrowseController.showTopicContents()}',
						{
							'topicId' : topicId,
							'userName' : user
						},
						function(data) {
							var ideas = data.ideas;
							var ideasPart = "";
							if (ideas == null || ideas == "") {
								document.getElementById(desiredTopic).innerHTML = "No ideas to show";
							} else {
								var y = ideas.split("&&");
								for ( var j = 0; j < y.length - 1; j = j + 1) {
									var id = y[j].split("-")[0];
									var title = y[j].split("-")[1];
									ideasPart += "<p><a href=\"@{TopicIdeaController.checkAuthorizationToViewIdea()}?ideaId="
											+ id + "\">" + title + "</a></p>";
								}
								document.getElementById(desiredTopic).innerHTML = "";
								document.getElementById(desiredTopic).innerHTML = ideasPart;
								$('#' + desiredTopic + '').hide();
								$('#' + desiredTopic + '').slideDown(1000);
							}

						});
	}
</script>

<script type="text/javascript">
	function showEntitOption(entitDiv) {
		if (entitDiv == "joinedEntits") {
			$('#followedEntits').hide(1000);
			$('#managedEntits').hide(1000);
			$('#joinedEntits').toggle(1000);
		} else if (entitDiv == 'followedEntits') {
			$('#joinedEntits').hide(1000);
			$('#managedEntits').hide(1000);
			$('#followedEntits').toggle(1000);
		} else if (entitDiv == 'managedEntits') {
			$('#joinedEntits').hide(1000);
			$('#followedEntits').hide(1000);
			$('#managedEntits').toggle(1000);
		} else {
			$('#joinedEntits').hide(1000);
			$('#followedEntits').hide(1000);
			$('#managedEntits').hide(1000);
		}
	}
</script>

</head>
<body>
	<p id="entOptions">
		<a href='#' onclick="showEntitOption('joinedEntits')"> Joined
			Entities</a> <a href='#' onclick="showEntitOption('followedEntits')">
			Followed Entities</a> <a href='#'
			onclick="showEntitOption('managedEntits')"> Managed Entities</a>
	</p>

	<div id="joinedEntits"></div>
	<div id="managedEntits"></div>
	<div id="followedEntits"></div>
</body>
